package common

import (
	"github.com/opentracing/opentracing-go"
	"github.com/uber/jaeger-client-go"
	jaegercfg "github.com/uber/jaeger-client-go/config"
	"io"
	"time"
	"strconv"
)

//根据生产环境 注册jaeger链路追踪实例
func RegTracer(serviceName string, port int64, mode string) (opentracing.Tracer, io.Closer, error){
	var url = "127.0.0.1"
	if mode == "docker" {
		url = "jaeger-url"
	}
	return NewTracer(serviceName, url+":"+strconv.FormatInt(port,10))
}

//创建jaeger链路追踪实例
func NewTracer(serviceName string, addr string) (opentracing.Tracer, io.Closer, error) {
	cfg := &jaegercfg.Configuration{
		ServiceName: serviceName,
		Sampler: &jaegercfg.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},
		Reporter: &jaegercfg.ReporterConfig{
			LogSpans:            true,
			BufferFlushInterval: 1 * time.Second,
			LocalAgentHostPort:  addr,
		},
	}
	tracer, closer, err := cfg.NewTracer()
	return tracer, closer, err
}
